package com.sevennotes.classtable.ui.pages.components

import android.graphics.fonts.FontStyle
import androidx.compose.foundation.gestures.detectDragGestures
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.size
import androidx.compose.material.FloatingActionButton
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import com.sevennotes.classtable.R
import androidx.compose.material3.Icon
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.pointer.consumeAllChanges
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.layout.positionInWindow
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.sevennotes.classtable.ui.pages.NavPage
import com.sevennotes.classtable.utils.contentColor
import kotlin.math.roundToInt

@Composable
fun MyFloatingButton(
  onClick: () -> Unit,
  icon: @Composable () -> Unit
) {
  val screenWidth = LocalConfiguration.current.screenWidthDp.dp
  val screenHeight = LocalConfiguration.current.screenHeightDp.dp
  val screenWidthInPx = with(LocalDensity.current) { screenWidth.toPx() }
  val screenHeightInPx = with(LocalDensity.current) { screenHeight.toPx() }
  val minXpx = with(LocalDensity.current) { 10.dp.toPx() }
  val minYpx = with(LocalDensity.current) { 80.dp.toPx() }
  val maxXpx = screenWidthInPx - with(LocalDensity.current) { 80.dp.toPx() }
  val maxYpx = screenHeightInPx - with(LocalDensity.current) { 80.dp.toPx() }
  var offsetX by remember { mutableStateOf(0f) }
  var offsetY by remember { mutableStateOf(0f) }
  FloatingActionButton(
    modifier = Modifier
        .offset { IntOffset(offsetX.roundToInt(), offsetY.roundToInt()) }
        .onGloballyPositioned {
            val sp = it.positionInWindow()
            if (sp.x < minXpx) offsetX -= sp.x - minXpx
            if (sp.x > maxXpx) offsetX -= sp.x - maxXpx
            if (sp.y < minYpx) offsetY -= sp.y - minYpx
            if (sp.y > maxYpx) offsetY -= sp.y - maxYpx
        }
        .pointerInput("floatButton") {
            detectDragGestures { change, dragAmount ->
                change.consume()
                offsetX += dragAmount.x
                offsetY += dragAmount.y
            }
        },
    onClick = onClick,
    content = icon
  )
}

@Composable
fun MyAddFloatingButton(onClick: () -> Unit) {
  MyFloatingButton(onClick = onClick) {
    Icon(imageVector = Icons.Filled.Add, contentDescription = null, tint = contentColor(MaterialTheme.colors.primary))
  }
}
